package com.yc.study.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yc.study.entity.CheckInTask;
import com.yc.study.entity.User;
import com.yc.study.service.ICheckInTaskService;
import com.yc.study.service.IUserService;
import com.yc.study.vo.CheckInTaskVo;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.Arrays;
import java.util.List;

/**
 * 签到任务 前端控制器
 *
 * @author 王妍
 * @since 2022-05-14
 */
@RestController
@RequestMapping("/study/checkInTask")
@AllArgsConstructor
public class CheckInTaskController {

  private final ICheckInTaskService checkInTaskService;

  private final IUserService userService;

  /**
   * 通过ids 删除 签到任务
   */
  @DeleteMapping
  public void deleteByIds(@RequestBody Long[] ids) {
    checkInTaskService.removeBatchByIds(Arrays.asList(ids));
  }

  /**
   * 分页查询
   */
  @GetMapping("page")
  public Page<CheckInTaskVo> getVoPage(Principal principal, Page<CheckInTaskVo> checkInTaskVoPage, CheckInTaskVo checkInTaskVo) {
    final User teacher = userService.loadUserByUserName(principal.getName());
    return checkInTaskService.pageVo(teacher.getId(),checkInTaskVoPage,checkInTaskVo);
  }

  /**
   * 保存签到任务
   */
  @PostMapping
  public void save(@RequestBody CheckInTask checkInTask ){
    checkInTaskService.saveOrUpdate(checkInTask);
  }


  @GetMapping("checkInTask")
  public List<CheckInTaskVo> getList(Principal principal,Long courseId){
    final String name = principal.getName();
    final User user = userService.loadUserByUserName(name);
    return  checkInTaskService.getList(user.getId(),courseId);
  }
}
